import { getBaseUrl } from '@/services/urlconst';
import { getUserID, requestFunComponFiex } from '@/utils';
import { useRequest } from 'umi';

const baseUrl = getBaseUrl();

/**
 * 办理流程
 * completeCallBack 回调
 */
const completeService = (completeCallBack: (res: any) => void, controller: string) => {
  const complete = useRequest(
    (params: any) => {
      const personUrl = `${baseUrl}/${controller}/complete`;
      let bodyParams = {
        ...params,
        assigneeID: getUserID(),
      };
      return requestFunComponFiex('PUT', personUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: completeCallBack,
    },
  );

  return {
    completeRun: complete?.run,
    completeLoading: complete?.loading,
  };
};

/**
 * 流程帮办
 * resolveTaskCallBack 回调
 */
const resolveTaskService = (resolveTaskCallBack: (res: any) => void, controller: string) => {
  const resolveTask = useRequest(
    (params: any) => {
      const personUrl = `${baseUrl}/${controller}/resolveTask`;
      let bodyParams = {
        ...params,
        assigneeID: getUserID(),
      };
      return requestFunComponFiex('PUT', personUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: resolveTaskCallBack,
    },
  );

  return {
    resolveTaskRun: resolveTask?.run,
    resolveTaskLoading: resolveTask?.loading,
  };
};

/**
 * 撤销流程
 * revokeCallBack 回调
 */
const revokeService = (revokeCallBack: (res: any) => void, controller: string) => {
  const revoke = useRequest(
    (params: any) => {
      let personUrl = `${baseUrl}/${controller}/revoke`;
      let bodyParams = {
        ...params,
      };
      return requestFunComponFiex('PUT', personUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: revokeCallBack,
    },
  );

  return {
    revokeRun: revoke?.run,
    revokeLoading: revoke?.loading,
  };
};

/**
 * 挂起流程
 * stopProcInstCallBack 回调
 */
const stopProcInstService = (stopProcInstCallBack: (res: any) => void, controller: string) => {
  const stopProcInst = useRequest(
    (params: any) => {
      let personUrl = `${baseUrl}/${controller}/stopProcInst`;
      let bodyParams = {
        ...params,
      };
      return requestFunComponFiex('PUT', personUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: stopProcInstCallBack,
    },
  );

  return {
    stopProcInstRun: stopProcInst?.run,
    stopProcInstLoading: stopProcInst?.loading,
  };
};

/**
 * 审核作废
 * invalidCallBack 回调
 */
const invalidService = (invalidCallBack: (res: any) => void, controller: string) => {
  const invalid = useRequest(
    (params: any) => {
      let personUrl = `${baseUrl}/${controller}/invalid`;
      let bodyParams = {
        ...params,
      };
      return requestFunComponFiex('PUT', personUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: invalidCallBack,
    },
  );

  return {
    invalidRun: invalid?.run,
    invalidLoading: invalid?.loading,
  };
};

/**
 * 推送审核记录到档案
 * */
/**同步PDF*/
const syncPDFtoOAServices = (cb: (res: Record<string, any>) => void, controller: string) => {
  const request = useRequest(
    (payload) => {
      let reqParams = { ...payload };
      let reqUrl = `${baseUrl}/${controller}/syncWorkFlow`;
      return requestFunComponFiex('POST', reqUrl, reqParams);
    },
    {
      manual: true,
      onSuccess: cb,
    },
  );

  return {
    syncPDFtoOARun: request?.run,
    syncPDFtoOALoading: request?.loading,
  };
};

/**
 * 加签接口
 * */
const addTransactorService = (cb: (res: Record<string, any>) => void, controller: string) => {
  const request = useRequest(
    (payload) => {
      let reqParams = { ...payload };
      delete reqParams?.['msequenceFlowList'];
      let reqUrl = `${baseUrl}/${controller}/addMuliti`;
      return requestFunComponFiex('POST', reqUrl, reqParams);
    },
    {
      manual: true,
      onSuccess: cb,
    },
  );

  return {
    addTransactorRun: request?.run,
    addTransactorLoading: request?.loading,
  };
};

/**
 * 校验是否可以撤回接口
 * */
const verifyRevocaService = (cb: (res: Record<string, any>) => void, controller: string) => {
  const request = useRequest(
    (payload) => {
      let reqParams = { ...payload };
      let reqUrl = `${baseUrl}/${controller}/checkDealFlag`;
      return requestFunComponFiex('PUT', reqUrl, reqParams);
    },
    {
      manual: true,
      onSuccess: cb,
    },
  );

  return {
    verifyRevocaRun: request?.run,
    verifyRevocaLoading: request?.loading,
  };
};

export {
  completeService,
  revokeService,
  stopProcInstService,
  invalidService,
  resolveTaskService,
  syncPDFtoOAServices,
  addTransactorService,
  verifyRevocaService,
};
